#!/bin/sh

# dirsync
#
# Copies files from dir1 to dir2 when the system starts,
# copies files from dir2 to dir1 when the system stops.
# I wrote this script to backup files in ramfs directories so 
# that not all data is lost at rebooting the system.
#
# create runlevel symlinks with command:
#    # update-rc.d -f dirsync defaults
#
# Version: v1.0
# Author: M.Post
# Last modified: 29 September 2011

CONFIGFILE=/etc/dirsync.conf

if ! test -f $CONFIGFILE
then
  echo "# dirsync.conf" >> $CONFIGFILE
  echo "#" >> $CONFIGFILE
  echo "# This file contains a list of directories that should be" >> $CONFIGFILE
  echo "# synchronised at system reboots. Ideal when using ramfs." >> $CONFIGFILE
  echo "#" >> $CONFIGFILE
  echo "# Format:" >> $CONFIGFILE
  echo "# <workdir>            <backupdir>" >> $CONFIGFILE
  echo "#" >> $CONFIGFILE
  echo "# Example:" >> $CONFIGFILE
  echo "# /dev/shm/rrd/        /var/lib/rrd/" >> $CONFIGFILE
  chown root:root $CONFIGFILE
  chmod 644 $CONFIGFILE
fi

do_stop () {

  # find directories from config file

  MAXLINES=`wc -l $CONFIGFILE | awk '{print $1}'`
  LINENR=1

  while test $LINENR -le $MAXLINES
  do
    READLINE=`head -n $LINENR $CONFIGFILE | tail -n 1`

    # ignore comment lines and empty lines
    if [ $(echo $READLINE|cut -c1-1) != "#" ]  > /dev/null 2>&1
    then
      
      # need two paths
      if ! test -z `echo "$READLINE" | awk '{print $2}'` 
      then

        # (at least) two entries found on one line, extract and test paths
        WORKDIR=`echo $READLINE | awk '{print $1}'`
        BKUPDIR=`echo $READLINE | awk '{print $2}'`

        # both paths must not be the same
        if [ "$WORKDIR" != "$BKUPDIR" ]
        then 

          # check for valid paths

          if test -d $WORKDIR && test -d $BKUPDIR
          then

            # paths ok
            echo -n "synching $WORKDIR to $BKUPDIR.. "
            # only copy when source is newer than target

            if rsync -aur $WORKDIR/. $BKUPDIR/ > /dev/null 2>&1
            then 
              echo "Ok"
            else
              echo "Failed"
            fi

          fi

        fi

      fi

    fi

    LINENR=$((LINENR + 1))

  done

}

do_start () {

  # find directories from config file

  MAXLINES=`wc -l $CONFIGFILE | awk '{print $1}'`
  LINENR=1

  while test $LINENR -le $MAXLINES
  do
    READLINE=`head -n $LINENR $CONFIGFILE | tail -n 1`

    # ignore comment lines and empty lines
    if [ $(echo $READLINE|cut -c1-1) != "#" ] > /dev/null 2>&1
    then
 
      # need two paths
      if ! test -z `echo "$READLINE" | awk '{print $2}'` 
      then

        # (at least) two entries found on one line, extract and test paths
        WORKDIR=`echo $READLINE | awk '{print $1}'`
        BKUPDIR=`echo $READLINE | awk '{print $2}'`

        # both paths must not be the same
        if [ "$WORKDIR" != "$BKUPDIR" ]
        then 

          # check for valid source paths
          # NOTE: We're copying from the BKUPDIR (source) to the WORKDIR (target)
          #       so only the source dir need to exist.

          if test -d $BKUPDIR
          then

            # path ok
            echo -n "synching $BKUPDIR to $WORKDIR.. "
            # only copy when source is newer than target
             
            if rsync -aur $BKUPDIR/. $WORKDIR/ > /dev/null 2>&1
            then 
              echo "Ok"
            else
              echo "Failed"
            fi

          fi

        fi

      fi

    fi

    LINENR=$((LINENR + 1))

  done

}


# original syntax
#sync -au --exclude-from=$BKUPDIR/.dirsync-exclude $BKUPDIR/{*,.[^.]*} $WORKDIR 

case "$1" in
  start)
	do_start
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop|update)
	do_stop
	;;
  *)
	echo "Usage: $0 start|update|stop" >&2
	exit 3
	;;
esac


